import uuid
from app.utils.timetool import *
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from werkzeug.security import generate_password_hash, check_password_hash


def make_uuid(value=False):
    if value:
        the_uuid = uuid.uuid1()
    else:
        the_uuid = uuid.uuid4()
    return str(the_uuid)


def user_token(uid, ex=None):
    """
        生成token
        :param uid: 用户id
        :param expires_in: 储存时间
        :return: 当 expires_in 为 None 时 解码
                 当 expires_in 为 int类型 时 token码
    """
    serializer = Serializer(str(str_change_timestamp(time.strftime('%Y-%m-%d'), "%Y-%m-%d")), expires_in=ex)
    # 接收用户id转换与编码
    if ex:
        data_txt = serializer.dumps({"id": uid}).decode("utf-8")
    else:
        try:
            # 转换为字典
            data_txt = serializer.loads(uid)
        except ArithmeticError:
            return None
    return data_txt
